package com.bjsxt.userscenter.outer.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.util.ConstatFinalUtil;
import com.bjsxt.userscenter.common.util.DateFormatUtil;
import com.bjsxt.userscenter.outer.service.IOuterService;
import com.bjsxt.userscenter.system.pojo.ARegion;
import com.bjsxt.userscenter.system.service.ISystemService;

/**
 * 外部请求的接口
 * 
 * @author wangshSxt
 *
 */
@Service("outerService")
public class OuterServiceImpl implements IOuterService
{
	@Resource
	private ISystemService systemService;

	@Override
	public JSONObject validatorAdmins01Service(JSONObject reqJSON)
	{
		DateFormatUtil dateFormatUtil = new DateFormatUtil();
		JSONObject dataReqJSON = reqJSON.getJSONObject("data");

		JSONObject resultJSON = new JSONObject();
		/* 下行的data中的json  */
		JSONObject dataResJSON = new JSONObject();
		/* 从上行的信息中取得token */
		String token = dataReqJSON.getString("token") + "";
		/* 从token容器中获取管理员的json信息 */
		JSONObject adminsJSON = ConstatFinalUtil.TOKENS_MAP.get(token);
		if (adminsJSON != null)
		{
			/* 拼装客户端需要的json信息(按照接口文档拼装) */
			/* 存储当前时间 */
			adminsJSON.put("validate", dateFormatUtil.format(new Date()));

			dataResJSON.put("admins", adminsJSON);
			resultJSON.put("data", dataResJSON);

			resultJSON.put("version", reqJSON.getString("version"));
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} else
		{
			resultJSON.put("code", "10");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("10"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject regionList01Service(JSONObject reqJSON)
	{
		/* 返回结果 */
		JSONObject resultJSON = new JSONObject();
		
		JSONObject dataReqJSON = reqJSON.getJSONObject("data");
		String parentId = dataReqJSON.getString("parentId");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据当前节点查询子节点 */
		condMap.clear();
		condMap.put("parentId", parentId);
		List<ARegion> childRegionList = this.systemService.findCondListRegionService(null, condMap);
		JSONArray regionList = new JSONArray();
		for (Iterator iterator = childRegionList.iterator(); iterator.hasNext();)
		{
			ARegion regionChild = (ARegion) iterator.next();
			JSONObject regionChildJSON = regionChild.toJSON() ; 
			regionList.add(regionChildJSON);
		}
		
		JSONObject dataResJSON = new JSONObject();
		dataResJSON.put("regionList", regionList);
		
		resultJSON.put("code", "0");
		resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		resultJSON.put("data", dataResJSON);
		return resultJSON;
	}

}
